package cc.mrbird.febs.payment.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.payment.entity.RefundInfo;
import cc.mrbird.febs.payment.service.IRefundInfoService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 退款记录 Controller
 *
 * @author Mr.Yang
 * @date 2022-08-12 09:32:44
 */
@Slf4j
@Validated
@Controller
public class RefundInfoController extends BaseController {

    @Autowired
    private IRefundInfoService refundInfoService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "refundInfo")
    public String refundInfoIndex(){
        return FebsUtil.view("refundInfo/refundInfo");
    }

    @GetMapping("refundInfo")
    @ResponseBody
    @RequiresPermissions("refundInfo:list")
    public FebsResponse getAllRefundInfos(RefundInfo refundInfo) {
        return new FebsResponse().success().data(refundInfoService.findRefundInfos(refundInfo));
    }

    @GetMapping("refundInfo/list")
    @ResponseBody
    @RequiresPermissions("refundInfo:list")
    public FebsResponse refundInfoList(QueryRequest request, RefundInfo refundInfo) {
        Map<String, Object> dataTable = getDataTable(this.refundInfoService.findRefundInfos(request, refundInfo));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增RefundInfo", exceptionMessage = "新增RefundInfo失败")
    @PostMapping("refundInfo")
    @ResponseBody
    @RequiresPermissions("refundInfo:add")
    public FebsResponse addRefundInfo(@Valid RefundInfo refundInfo) {
        this.refundInfoService.createRefundInfo(refundInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除RefundInfo", exceptionMessage = "删除RefundInfo失败")
    @GetMapping("refundInfo/delete")
    @ResponseBody
    @RequiresPermissions("refundInfo:delete")
    public FebsResponse deleteRefundInfo(RefundInfo refundInfo) {
        this.refundInfoService.deleteRefundInfo(refundInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改RefundInfo", exceptionMessage = "修改RefundInfo失败")
    @PostMapping("refundInfo/update")
    @ResponseBody
    @RequiresPermissions("refundInfo:update")
    public FebsResponse updateRefundInfo(RefundInfo refundInfo) {
        this.refundInfoService.updateRefundInfo(refundInfo);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改RefundInfo", exceptionMessage = "导出Excel失败")
    @PostMapping("refundInfo/excel")
    @ResponseBody
    @RequiresPermissions("refundInfo:export")
    public void export(QueryRequest queryRequest, RefundInfo refundInfo, HttpServletResponse response) {
        List<RefundInfo> refundInfos = this.refundInfoService.findRefundInfos(queryRequest, refundInfo).getRecords();
        ExcelKit.$Export(RefundInfo.class, response).downXlsx(refundInfos, false);
    }
}
